#include "imcommon.h"
//#include "XmlStorer.h"

char *deal_friends(char *source)
{     if (source == NULL)
    return NULL;
    char *str = strdup(source);
    int result;
    if (result != -1)
        result = cns_str_ereplace(&str, "subscription=\".*?\"", "");
    if (result != -1)
        result = cns_str_ereplace(&str, "ros:.*?=\".*?\"", "");
    if (result != -1)
        result = cns_str_ereplace(&str, "<item jid", "");
    if (result != -1)
        result = cns_str_ereplace(&str, "=", "");
    if (result != -1)
        result = cns_str_ereplace(&str, "/>", ",");
    if (result != -1)
        result = cns_str_ereplace(&str, " ", "");
    if (result != -1)
        result = cns_str_ereplace(&str, "\"", "");
    return str;
}

time_t str_to_time(char *tmp_time)
{
    struct tm t;
    
}

int get_gtalk_friends(MsgNode *gtalk_node)
{
/*
    < iq to="XiYingIT@gmail.com/Talk.v1078BDC4A9A" id="16" type="result">
    <query ros:ext="2" xmlns="jabber:iq:roster" xmlns:ros="google:roster">
    <item jid="kenshinittest@gmail.com" subscription="both" ros:w="6" ros:mc="26"/>
    <item jid="554101587@qq.com" subscription="none" ros:w="0" ros:emc="1"/>
    <item jid="fengjie1051@gmail.com" subscription="both" ros:w="0" ros:mc="2"/>
    <item jid="huangb0910200213@gmail.com" subscription="both"/></query></iq>
*/
    char *pstart = NULL;
    char *pend   = NULL;
    
    char patternfriends[] = "<item jid=(.*)/>";
    //printf("data:%s\n",gtalk_node->data);

    char *strtmp = NULL;
    regcompile_2(gtalk_node->data, patternfriends, &strtmp);
    if (NULL == strtmp)
        return -1;
    gtalk_node->friends = deal_friends(strtmp);
    
    pstart = strstr(gtalk_node->data, "<iq to=\"");
    if (NULL == pstart)
    {
        return -1;
    }
    pstart += 8;
    pend = strstr(pstart, "@gmail.com");
    if (NULL == pend)
    {
        return -1;
    }
    pend += 10;
    memset(gtalk_node->username,0,MAX_UN_LEN);
    strncpy(gtalk_node->username,pstart,pend-pstart);
    gtalk_node->username[pend-pstart+1] = '\0';

    pstart = strstr(pend,"id=\"");
    if (NULL == pstart)
    {
        return -1;
    }
    OUT_PUT;
    pstart += 4;
    pend = strstr(pstart,"\"");
    if (NULL == pend)
    {
        return -1;
    }
    OUT_PUT;
    gtalk_node->Id= (char *)malloc(pend-pstart+1);
    memset(gtalk_node->Id,0,pend-pstart+1);
    strncpy(gtalk_node->Id,pstart,pend-pstart);
    gtalk_node->Id[pend-pstart+1] = '\0'; 
    memset(gtalk_id, 0, 40);
    memcpy(gtalk_id, gtalk_node->Id, pend-pstart+1);
    
    gtalk_node->msgType = Login;
    printf("friends:%s\n",gtalk_node->friends);
    output_msg(gtalk_node);
    
 //   free(strtmp);
    free(gtalk_node->Id);
    gtalk_node->Id = NULL;
    del_MsgNode(gtalk_node);
    return 0;
}

int analyse_gtalk_Login( MsgNode *gtalk_node)
{
     /*
    <iq id="0" type="result"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind">
    <jid>Kenshinittest@gmail.com/Talk.v107830308E3</jid></bind></iq>
     */
    char *pstart = NULL;
    char *pend   = NULL;
    pstart = strstr(gtalk_node->data, "<jid>");
    if (NULL == pstart)
    {
        return -1;
    }
    pstart += 5;
    pend = strstr(pstart, "@gmail.com");
    if (NULL == pend)
    {
        return -1;
    }
    pend += 10;
    memset(gtalk_node->username,0,MAX_UN_LEN);
    strncpy(gtalk_node->username,pstart,pend-pstart);
    gtalk_node->username[pend-pstart+1] = '\0';
    
    gtalk_node->Id= (char *)malloc(2);
    memcpy(gtalk_node->Id,"0",1);

    memset(gtalk_id, 0, 40);
    memcpy(gtalk_id, gtalk_node->Id, pend-pstart+1);
    gtalk_node->msgType = Login;
   // if (NULL != p)
       // del_user(p);
   usr_record *p = search_user(user_record,gtalk_node->destMac, "@gmail.com");
    if (p != NULL)
        delete(p);
    free(p);
    p = NULL;
    insert_user(user_record,gtalk_node->destMac, gtalk_node->username);
    output_msg(gtalk_node);
    free(gtalk_node->Id);
    gtalk_node->Id = NULL;
    return 0;
}

int write_gtalk_Send(MsgNode *gtalk_node)
{
    /*
    <message to="fengjie1051@gmail.com" type
    ="chat" id="64">
    <body>test2</body>
    <active xmlns="http://jabber.org/protocol/chatstates"/>
    </message>
      */
    char *pstart = NULL;
    char *pend = NULL;

  //  printf("data:%s\n",gtalk_node->data);
    pstart = strstr(gtalk_node->data,"<message to=\"");
    if (NULL == pstart)
    {
        return -1;
    }
    pstart += strlen("<message to=\"");
    pend = strstr(pstart,"@gmail.com");
    if (NULL == pend)
        return -1;
    pend += 10;
    strncpy(gtalk_node->to,pstart,pend-pstart);
    gtalk_node->to[pend-pstart+1] = '\0';
    
    pstart = strstr(pend,"id=\"");
    if (NULL == pstart)
    {
        return -1;
    }
    pstart += 4;
    pend = strstr(pstart,"\"");
    if (NULL == pend)
    {
        return -1;
    }
    gtalk_node->Id= (char *)malloc(pend-pstart+1);
    memset(gtalk_node->Id,0,pend-pstart+1);
    strncpy(gtalk_node->Id,pstart,pend-pstart);
    gtalk_node->Id[pend-pstart+1] = '\0'; 
    memset(gtalk_id, 0, 40);
    memcpy(gtalk_id, gtalk_node->Id, pend-pstart+1);
    
    pstart = strstr(pend,"<body>");
    if (NULL == pstart)
    {
        return -1;
    }
    pstart += 6;
    pend = strstr(pstart,"</body>");
    if (NULL == pend)
    {
        return -1;
    }
    
    gtalk_node->content = (char *)malloc(pend-pstart+1);
    memset(gtalk_node->content,0,pend-pstart+1);
    strncpy(gtalk_node->content,pstart,pend-pstart);
    gtalk_node->content[pend-pstart+1] = '\0';
    char *str_tmp = clear_html_symbol(gtalk_node->content);
    free(gtalk_node->content);
    gtalk_node->content = str_tmp;
    
    gtalk_node->msgType = Text;
    gtalk_node->s_or_r = 0;
    usr_record *p = search_user(user_record,gtalk_node->srcMac, "@gmail.com");
    if (p)
    {
        strcpy(gtalk_node->from,p->username);
        strcpy(gtalk_node->username,p->username);
    }
    output_msg(gtalk_node);
    free(gtalk_node->Id);
    //gtalk_node->Id = NULL;
    free(str_tmp);
    //str_tmp = NULL;
    return 0;
}

int write_gtalk_Rec(MsgNode *gtalk_node)
{
    /*
    <message to="kenshinittest@gmail.com" type
    ="chat" id="7922EAC16C1F03FA_0" iconset
    ="classic" from="xiyingit@gmail.com/gmail.0D9FE3CA">
    <body>testhello</body>
    <met:google-mail-signature xmlns:met
    ="google:metadata">gtm4t5FU8XpDrq34YbYPu1TYijY</met:google-mail-signature>
    <cha:active xmlns:cha="http://jabber.org/protocol/chatstates"/>
    <nos:x value="disabled" xmlns:nos="google:nosave"/>
    <arc:record otr="false" xmlns:arc="http://jabber.org/protocol/archive"/>
    </message>
      */
      
    char *pstart = NULL;
    char *pend = NULL;
       // printf("data:%s\n",gtalk_node->data);
    pstart = strstr(gtalk_node->data,"<message to=\"");
    if (NULL == pstart)
    {
        return -1;
    }
    pstart += strlen("<message to=\"");
    pend = strstr(pstart,"@gmail.com");
    if (NULL == pend)
    {
        return -1;
    }  
    pend += 10;
    strncpy(gtalk_node->to,pstart,pend-pstart);
    gtalk_node->to[pend-pstart+1] = '\0';
    memcpy(gtalk_node->username,gtalk_node->to,pend-pstart+1);
   
    pstart = strstr(pend,"id=\"");
    if (NULL == pstart)
    {
        return -1;
    }
    pstart += 4;
    pend = strstr(pstart,"\"");
    if (NULL == pend)
    {
        return -1;
    }
    gtalk_node->Id= (char *)malloc(pend-pstart+1);
    memset(gtalk_node->Id,0,pend-pstart+1);
    strncpy(gtalk_node->Id,pstart,pend-pstart);
    gtalk_node->Id[pend-pstart+1] = '\0'; 

    pstart = strstr(pend,"from=\"");
    if (NULL == pstart)
    {
        return -1;
    }
    pstart += 6;
    pend = strstr(pstart,"@gmail.com");
    if (NULL == pend)
    {
        return -1;
    }
    pend += 10;
    strncpy(gtalk_node->from,pstart,pend-pstart);
    gtalk_node->from[pend-pstart+1] = '\0';
    
    pstart = strstr(pend,"<body>");
    if (NULL == pstart)
    {
        return -1;
    }
    pstart += 6;
    pend = strstr(pstart,"</body>");
    if (NULL == pend)
    {
        return -1;
    }
    
    gtalk_node->content = (char *)malloc(pend-pstart+1);
    memset(gtalk_node->content,0,pend-pstart+1);
    strncpy(gtalk_node->content,pstart,pend-pstart);
    gtalk_node->content[pend-pstart+1] = '\0';
    char *str_tmp = clear_html_symbol(gtalk_node->content);
    free(gtalk_node->content);
    gtalk_node->content = str_tmp;
    
 //   gtalk_node->content = str_tmp;
    memset(gtalk_id, 0, 40);
    memcpy(gtalk_id, gtalk_node->Id, pend-pstart+1);
    gtalk_node->msgType = Text;
    gtalk_node->s_or_r = 1;
    output_msg(gtalk_node);
    free(gtalk_node->Id);
    free(str_tmp);
    //gtalk_node->Id = NULL;
   // free(gtalk_node->content);
   
    return 0;
}

int analyse_gtalk_Logout( MsgNode *gtalk_node)
{
    static int Logout_flag = 0;
    usr_record *p = search_user(user_record,gtalk_node->srcMac, "@gmail.com");
    if (NULL != p)
        strcpy(gtalk_node->username,user_record->username);
    gtalk_node->msgType = Logout;
    struct tm *ptm = localtime(&(gtalk_node->time));
    if (++Logout_flag == 1)
    {
        printf("%s is exiting\n",gtalk_node->username);
        printf("exited time is:%u-%u-%u %u:%02u\n", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday,ptm->tm_hour, ptm->tm_min);
        if (NULL != p)
            del_user(p);
        return 0;
    }
    if(Logout_flag == 4)
    {
        printf("%s exited!\n",gtalk_node->username);
        printf("exited time is:%u-%u-%u %u:%02u\n", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday,ptm->tm_hour, ptm->tm_min);
        output_msg(gtalk_node);
    if (NULL != p)
            del_user(p);
    }
    
    return 0;
}

int write_gtalk_offline_msg(MsgNode *gtalk_node)
{
/*
    message to="kenshinittest@gmail.com" iconset="classic" from="xiyingit@gmail.com"><body>test1</body>
    <met:google-mail-signature xmlns:met="google:metadata">EH8d2BO_ZpDqSeXjPH5MTGq6jPw
    </met:google-mail-signature><x stamp="20120823T06:19:24" xmlns="jabber:x:delay"/>
    <time ms="1345702764473" xmlns="google:timestamp"/></message>
  */
    char *pstart = NULL;
    char *pend = NULL;
       // printf("data:%s\n",gtalk_node->data);
    pstart = strstr(gtalk_node->data,"<message to=\"");
    if (NULL == pstart)
    {
        return -1;
    }
    pstart += strlen("<message to=\"");
    pend = strstr(pstart,"@gmail.com");
    if (NULL == pend)
    {
        return -1;
    }  
    pend += 10;
    strncpy(gtalk_node->to,pstart,pend-pstart);
    gtalk_node->to[pend-pstart+1] = '\0';
    memcpy(gtalk_node->username,gtalk_node->to,pend-pstart+1);
    
    pstart = strstr(pend,"from=\"");
    if (NULL == pstart)
    {
        return -1;
    }
    pstart += 6;
    pend = strstr(pstart,"@gmail.com");
    if (NULL == pend)
    {
        return -1;
    }
    pend += 10;
    strncpy(gtalk_node->from,pstart,pend-pstart);
    gtalk_node->from[pend-pstart+1] = '\0';
    
    pstart = strstr(pend,"<body>");
    if (NULL == pstart)
    {
        return -1;
    }
    pstart += 6;
    pend = strstr(pstart,"</body>");
    if (NULL == pend)
    {
        return -1;
    }
    
    gtalk_node->content = (char *)malloc(pend-pstart+1);
    memset(gtalk_node->content,0,pend-pstart+1);
    strncpy(gtalk_node->content,pstart,pend-pstart);
    gtalk_node->content[pend-pstart+1] = '\0';
    char *str_tmp = clear_html_symbol(gtalk_node->content);
    free(gtalk_node->content);
    gtalk_node->content = str_tmp;
    
 //   gtalk_node->content = str_tmp;
    pstart = strstr(pend, "<x stamp=\"");
    if (NULL == pstart)
        return -1;
    pstart += 10;

    pend = strstr(pstart, "\"");

    char *tmp_time = (char *)malloc(pend - pstart +1);
    memset(tmp_time, 0 , pend-pstart+1);
    strncpy(tmp_time, pstart, pend-pstart);
    tmp_time[pend-pstart+1] = '\0';
    
    gtalk_node->msgType = Text;
    gtalk_node->s_or_r = 1;
    output_msg(gtalk_node);
    free(str_tmp);
    return 0;
}

